import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class TrangleFrame extends JFrame {
    private JLabel label1;
    private JTextField textField1;
    private JLabel label2;
    private JTextField textField2;
    private JButton button1;
    private JTextField textField3;
    //把数据写入数据库
    private Triangle triangle;

    public TrangleFrame() {
        label1 = new JLabel("length");
        textField1 = new JTextField(10);
        label2 = new JLabel("width");
        textField2 = new JTextField(10);
        button1 = new JButton("calculate");
        textField3 = new JTextField(10);
        triangle = new Triangle();

        this.setLayout(new GridLayout(3,2));
        this.add(label1);
        this.add(textField1);
        this.add(label2);
        this.add(textField2);
        this.add(button1);
        this.add(textField3);

        this.setVisible(true);
        this.setSize(400,300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //添加Button监听器
        button1.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                doButton1();
                writetoDb();
            }
        });
    }
    //获取输入框的值
    private void doButton1(){
        double length;
        double width;
        double area;
        length = Double.parseDouble(textField1.getText());
        width = Double.parseDouble(textField2.getText());
        area = (length * width) / 2;
        textField3.setText("Area" + area);

        triangle.setId(0);
        triangle.setName("苏子豪");
        triangle.setNumber("202205010347");
        triangle.setLength(length);
        triangle.setHeight(width);
        triangle.setArea(area);
        //生成现在创建的时间
        DateTimeFormatter formatter  = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String now = LocalDateTime.now().format(formatter);
        triangle.setNow(now);


    }
//测试数据库驱动是否正常
    private void writetoDb(){
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            System.out.println("数据库驱动加载成功");
            //连接数据库
//            jdbc:mysql://[hostname]:[port]/[databaseName]?[parameters]
            String url = "jdbc:mysql://localhost:3306/rgdb1";
            try {
                Connection coon = DriverManager.getConnection(url,"root","123456");
                System.out.println("Successsful");
                Statement stmt = coon.createStatement();
               String insert = "insert into triangle values("+triangle.getId()+",'"+triangle.getName()+"','"+triangle.getNumber()+"',"+triangle.getLength()+","+triangle.getHeight()+","+triangle.getArea()+",'"+triangle.getNow()+"')";
               int count = stmt.executeUpdate(insert);
               if(1 == count){
                   JOptionPane.showMessageDialog(this,"写入成功");
               }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}
